package com.yjc.rabbitmq.springboot_rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author shkstart
 * @create 2022-08-03 12:32
 */
@Configuration
public class ConfirmConfig {

    //普通队列
    public static final String CONFIRM_QUEUE = "confirm.queue";
    //普通交换机
    public static final String CONFIRM_EXCHANGE = "confirm.exchange";
    //RoutingKey
    public static final String CONFIRM_ROUTINGKEY = "key1";
    //备份交换机
    public static final String BACKUP_EXCHANGE = "backup.exchange";
    //备份队列
    public static final String BACKUP_QUEUE = "backup.queue";
    //报警队列
    public static final String WARNING_QUEUE = "warning.queue";

    @Bean("confirmQueue")
    public Queue confirmQueue() {
        return QueueBuilder.durable(CONFIRM_QUEUE).build();
    }

    @Bean("backupQueue")
    public Queue backupQueue() {
        return QueueBuilder.durable(BACKUP_QUEUE).build();
    }

    @Bean("warningQueue")
    public Queue warningQueue() {
        return QueueBuilder.durable(WARNING_QUEUE).build();
    }


    @Bean("confirmExchange")
    public DirectExchange confirmExchange() {
//        return new DirectExchange(CONFIRM_EXCHANGE);
        return ExchangeBuilder.directExchange(CONFIRM_EXCHANGE).withArgument("alternate-exchange", BACKUP_EXCHANGE).build();
    }


    @Bean("backupExchange")
    public FanoutExchange backupExchange() {
        return new FanoutExchange(BACKUP_EXCHANGE);
    }

    @Bean
    public Binding queueBingingExchange(Queue confirmQueue, DirectExchange confirmExchange) {
        return BindingBuilder.bind(confirmQueue).to(confirmExchange).with(CONFIRM_ROUTINGKEY);
    }

    @Bean
    public Binding queueBackBingingExchangeBack(Queue backupQueue, FanoutExchange backupExchange) {
        return BindingBuilder.bind(backupQueue).to(backupExchange);
    }

    @Bean
    public Binding queueWarningBingingExchangeBack(Queue warningQueue, FanoutExchange backupExchange) {
        return BindingBuilder.bind(warningQueue).to(backupExchange);
    }

}
